
package com.eyedog.video;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.eyedog.uilibrary.tabs.TabsActivity;
import com.eyedog.video.NotifyDialog.NotifyDialog;
import com.eyedog.video.adv.AdvInstance;
import com.eyedog.video.entity.ADVEntity;
import com.eyedog.video.maincategory.CategoryFragment;
import com.eyedog.video.mine.MineFragment;
import com.eyedog.video.webview.WebFragment;
import com.fm.openinstall.OpenInstall;
import com.fm.openinstall.listener.AppWakeUpAdapter;
import com.fm.openinstall.model.AppData;

public class MainActivity extends TabsActivity {

    private final String TAG = "MainActivity";

    CategoryFragment mainListFragment;

    WebFragment webViewFragment;

//    SearchFragment searchFragment;

    MineFragment mineFragment;

    public static Intent createIntent(Context context, int tab) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra("TAB", tab);
        //获取唤醒参数
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取唤醒参数
        mainListFragment = new CategoryFragment();
        webViewFragment = new WebFragment();
        ADVEntity advEntity = AdvInstance.getInstance().getGameAdv();
        if (advEntity != null) {
            Bundle args = WebFragment.createBundle(WebFragment.FROM_GAME, advEntity.url);
            if (args != null) {
                webViewFragment.setArguments(args);
            }
        }
//        searchFragment = new SearchFragment();
        mineFragment = new MineFragment();
        sendEmptyUIMessageDelay(MSG_UI_SHOW_NOTIFY, 3000);
        refreshTab(getIntent());
    }

    private void refreshTab(Intent intent){
        OpenInstall.getWakeUp(intent, wakeUpAdapter);
        int tab = intent.getIntExtra("TAB", 0);
        switch (tab) {
            case 0:
                refreshTabSelected(mTab1.getId());
                onSelecteTab1();
                break;
            case 1:
                refreshTabSelected(mTab2.getId());
                onSelecteTab2();
                break;
            case 2:
                refreshTabSelected(mTab3.getId());
                onSelecteTab3();
                break;
            default:
                refreshTabSelected(mTab1.getId());
                onSelecteTab1();
                break;
        }
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        refreshTab(intent);
    }

    @Override
    protected void onSelecteTab1() {
        super.onSelecteTab1();
        if (mainListFragment != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fl_content, mainListFragment).commit();
        }
    }

    @Override
    protected void onSelecteTab2() {
        super.onSelecteTab2();
        if (webViewFragment != null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, webViewFragment)
                    .commit();
        }
    }

    @Override
    protected void onSelecteTab3() {
        super.onSelecteTab3();
        if (mineFragment != null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, mineFragment)
                    .commit();
        }
    }

    private final int MSG_UI_SHOW_NOTIFY = 0x01;

    @Override
    protected void handleUIMessage(Message message) {
        super.handleUIMessage(message);
        switch (message.what) {
            case MSG_UI_SHOW_NOTIFY: {
                ADVEntity entity = AdvInstance.getInstance().getNotifyAdv();
                if (entity != null) {
                    NotifyDialog dialog = new NotifyDialog(this);
                    dialog.show();
                }
            }
            break;
        }
    }

    AppWakeUpAdapter wakeUpAdapter = new AppWakeUpAdapter() {
        @Override
        public void onWakeUp(AppData appData) {
            //获取渠道数据
            String channelCode = appData.getChannel();
            //获取绑定数据
            String bindData = appData.getData();
            Log.d("OpenInstall", "getWakeUp : wakeupData = " + appData.toString());
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        wakeUpAdapter = null;
    }
}
